#include <vector>

#include "uint.h"
#include "cCompressor.h"

/* Compresses data.
 * Inputs:
 * -data: Data to compress.
 * -out: Buffer to write to.
 * -size: Size of buffer in bytes.
 * Returns: Size of compressed data.
 */
u32 Compressor::Compress(u8* data, u8* out, u32 size)
{
	//we don't need an "is data" flag, just set data = NULL if not
	this->data = data;
	this->file = NULL;
	return this->_Compress(out, size);
}

//I'm not sure why you'd want to compress data from in a file, but oh well
u32 Compressor::Compress(FILE* file, u32 addr, u8* out, u32 size)
{
	this->file = file;
	this->data = NULL;
	this->baseaddr = addr;
	return this->_Compress(out, size);
}

/* Decompresses data.
 * Inputs:
 * -data: Data to decompress.
 * -out: Buffer to write to.
 * -size: Size of buffer in bytes.
 * Returns: Size of decompressed data.
 */
u32 Compressor::Decompress(u8* data, u8* out, u32 size)
{
	this->data = data;
	this->file = NULL;
	return this->_Decompress(out, size);
}

u32 Compressor::Decompress(FILE* file, u32 addr, u8* out, u32 size)
{
	this->file = file;
	this->data = NULL;
	this->baseaddr = addr;
	return this->_Decompress(out, size);
}

/* PROTECTED: Reads a byte from the stream or buffer.
 * Inputs:
 * -Pos: Byte to read.
 * Returns: Byte read.
 */
u8 Compressor::GetByte(u32 pos)
{
	u8 b;
	if(this->data) return this->data[pos];
	fseek(this->file, this->baseaddr + pos, SEEK_SET);
	if(!fread(&b, 1, 1, this->file)) b = 0;
	return b;
}
